<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="title">
  <h1>Audit</h1>
</div>
<mat-card class="api-audit__filters" [formGroup]="auditForm">
  <mat-card-content>
    <mat-form-field class="api-audit__filters__field">
      <mat-label>Event</mat-label>
      <mat-select formControlName="event">
        <mat-option></mat-option>
        <mat-option *ngFor="let name of eventsName$ | async" [value]="name">{{ name }}</mat-option>
      </mat-select>
    </mat-form-field>

    <mat-form-field class="api-audit__filters__field right">
      <mat-label>Enter a date range</mat-label>
      <mat-date-range-input formGroupName="range" [rangePicker]="picker">
        <input matStartDate formControlName="start" placeholder="Start date" />
        <input matEndDate formControlName="end" placeholder="End date" />
      </mat-date-range-input>
      <mat-hint>MM/DD/YYYY – MM/DD/YYYY</mat-hint>
      <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
      <mat-date-range-picker #picker></mat-date-range-picker>
      <mat-error *ngIf="auditForm.controls.range.hasError('matStartDateInvalid')">Invalid start date</mat-error>
      <mat-error *ngIf="auditForm.controls.range.hasError('matEndDateInvalid')">Invalid end date</mat-error>
    </mat-form-field>
  </mat-card-content>

  <gio-table-wrapper
    [disableSearchInput]="true"
    [length]="nbTotalAudit"
    [filters]="filtersStream.value.tableWrapper"
    (filtersChange)="onFiltersChanged($event)"
  >
    <table mat-table [dataSource]="auditList" class="api-audit__table" id="auditTable" aria-label="Audit table">
      <!-- Date Column -->
      <ng-container matColumnDef="date">
        <th mat-header-cell *matHeaderCellDef id="date" width="1%">Date</th>
        <td mat-cell *matCellDef="let element">{{ element.date | date: 'medium' }}</td>
      </ng-container>

      User Column
      <ng-container matColumnDef="user">
        <th mat-header-cell *matHeaderCellDef id="user" width="15%">User</th>
        <td mat-cell *matCellDef="let element">
          {{ element.user }}
        </td>
      </ng-container>

      <!-- Event Column -->
      <ng-container matColumnDef="event">
        <th mat-header-cell *matHeaderCellDef id="event" width="15%">Event</th>
        <td mat-cell *matCellDef="let element">{{ element.event }}</td>
      </ng-container>

      <!-- Targets Column -->
      <ng-container matColumnDef="targets">
        <th mat-header-cell *matHeaderCellDef id="targets" width="15%">Target</th>
        <td mat-cell *matCellDef="let element">
          <div class="targets" *ngFor="let target of element.targets | keyvalue">
            <div class="targets__target">{{ target.key }}:</div>
            <div>{{ target.value }}</div>
          </div>
        </td>
      </ng-container>

      <!-- Patch Column -->
      <ng-container matColumnDef="patch">
        <th mat-header-cell *matHeaderCellDef id="patch" width="1%">Patch</th>
        <td mat-cell *matCellDef="let element">
          <div class="pre-wrapper">
            <div>
              <button
                *ngIf="!element.displayPatch"
                (click)="element.displayPatch = true"
                mat-icon-button
                aria-label="Button to show patch"
                matTooltip="Show patch"
              >
                <mat-icon svgIcon="gio:eye-empty"></mat-icon>
              </button>

              <button
                *ngIf="element.displayPatch"
                (click)="element.displayPatch = false"
                mat-icon-button
                aria-label="Button to hide patch"
                matTooltip="Hide patch"
              >
                <mat-icon svgIcon="gio:cancel"></mat-icon>
              </button>
            </div>
            <div>
              <pre *ngIf="element.displayPatch">{{ element.patch | json }}</pre>
            </div>
          </div>
        </td>
      </ng-container>

      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>

      <!-- Row shown when there is no data -->
      <tr class="mat-mdc-row mdc-data-table__row" *matNoDataRow>
        <td class="mat-mdc-cell mdc-data-table__cell" [attr.colspan]="displayedColumns.length">No audit</td>
      </tr>
    </table>
  </gio-table-wrapper>
</mat-card>
